//
//  GGResetlaytouViewController.m
//  Gunterapple
//
//  Created by 热带雨林 on 2020/3/30.
//  Copyright © 2020 热带雨林. All rights reserved.
//

#import "GGResetlaytouViewController.h"
#import "GGPerson.h"
#import <malloc/malloc.h>
#import <objc/runtime.h>
#import "updataLayoutViewController.h"
@interface GGResetlaytouViewController ()<UIViewGDButtonDelegate>

@property (nonatomic, strong) UIView *redView;
@property (nonatomic, strong) UIButton *btn;
@property (nonatomic, strong) GGPerson *person;
@property (nonatomic, copy, readonly) NSString *name;
@property (nonatomic, strong) UIView *btnView;
@property (nonatomic, assign) CGRect btnRect;
@property (nonatomic, strong) MASConstraint *sizeContraint;
@property (nonatomic, strong) NSMutableArray *viewArray;
@property (nonatomic, strong) UIView *showView;
@property (nonatomic, strong) UILabel *lessLabel;
@property (nonatomic, strong) UILabel *greatLabel;

@property (nonatomic, strong) UILabel *leftLabel;
@property (nonatomic, strong) UILabel *rightLabel;


@end

@implementation GGResetlaytouViewController

@dynamic name;// 不会自动生成set 与 get 方法

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpUI];
}
- (void)setUpUI{
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self addtestView];
    self.btnView =  [[UIView alloc]initWithFrame:CGRectZero];
    [self.view addSubview:self.btnView];
    self.btnView.delegate = self;
    NSArray *btndataArray = @[@"重置约束(remake)",@"更新约束(update)",@"多行label的约束问题"];
    GDWeakSelf
    [self.btnView creatNormalButtonWithTitleArray:btndataArray withRect:^(CGRect rect) {
        weakSelf.btnRect = rect;
    }];
    [self.btnView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.redView.mas_bottom).and.offset(20);
        make.left.equalTo(self.view).offset(20);
        make.size.mas_equalTo(CGSizeMake(self.btnRect.size.width, self.btnRect.size.height));
    }];
    
    GGPerson *person = [[GGPerson alloc]init];
//    NSLog(@"%lu",malloc(person));
    //查看当前对象占多少个字节
    //注意此处的桥接
    NSLog(@"%zd", malloc_size((__bridge const void *)(person)));
    
    //此处是关于对象属性的字节数
    NSLog(@"%zd",sizeof(person));
    NSLog(@"hh");
    
#pragma mark - mas_greaterThanOrEqualTo  mas_lessThanOrEqualTo
    //天戴其苍，地履其黄。纵有千古，横有八荒。前途似海，来日方长。美哉我少年中国，与天不老！壮哉我中国少年，与国无疆！
    self.lessLabel = [[UILabel alloc]init];
    [self.view addSubview:self.lessLabel];
    self.lessLabel.text = @"天戴其苍，地履其黄。纵有千古，横有八荒。前途似海，来日方长。美哉我少年中国，与天不老！壮哉我中国少年，与国无疆！";
    self.lessLabel.backgroundColor = [UIColor redColor];
    [self.lessLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.btnView.mas_bottom).offset(20);
        make.left.equalTo(self.view).and.offset(20);
        //如果文字长度没有超过200，文字有多长就是实际长度，如果文字长度超过200，多余的部分就会被省略
        make.width.mas_lessThanOrEqualTo(200);
    }];
    
    self.greatLabel = [[UILabel alloc]init];
    [self.view addSubview:self.greatLabel];
    self.greatLabel.text = @"前途似海，来日方长";
    self.greatLabel.backgroundColor = [UIColor blueColor];
    [self.greatLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.lessLabel.mas_bottom).with.offset(20);
        make.left.equalTo(self.lessLabel);
        //如果文字没有超过200，那实际长度就是200，如果文字超过200，那实际长度就是文字的长度，不会被省略
        make.width.mas_greaterThanOrEqualTo(200);
        //此处是如果长度超过了屏幕的长度，右侧的距离距屏幕右侧相距10
        make.right.mas_lessThanOrEqualTo(self.view.mas_right).and.offset(-10);
    }];
    
    self.leftLabel = [[UILabel alloc]init];
    self.leftLabel.text = @"天戴其苍，地履其黄。纵有千古，横有八荒。";
    self.leftLabel.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.leftLabel];
    [self.leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.greatLabel.mas_bottom).and.offset(20);
        make.left.equalTo(self.greatLabel);
        make.width.mas_greaterThanOrEqualTo(100);//没超过就是300，超过了就按实际的宽度
    }];
    
    self.rightLabel = [[UILabel alloc]init];
    self.rightLabel.text = @"天戴其苍，地履其黄。纵有千古，横有八荒。前途似海，来日方长。";
    self.rightLabel.backgroundColor = [UIColor blueColor];
    [self.view addSubview:self.rightLabel];
    [self.rightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.leftLabel);
        make.left.equalTo(self.leftLabel.mas_right).offset(10);
        make.right.equalTo(self.view.mas_right).offset(-10);
//        make.width.mas_lessThanOrEqualTo(100);//没超过就好，超过了就是100的宽度
    }];
    
    //设置优先级  抗压缩
    //右侧优先级高，优先展示
    [self.leftLabel setContentCompressionResistancePriority:UILayoutConstraintAxisHorizontal forAxis:UILayoutPriorityDefaultLow];
    
    [self.rightLabel setContentCompressionResistancePriority:UILayoutConstraintAxisHorizontal forAxis:UILayoutPriorityDefaultHigh];
    
    
}

- (void)clickButtonWithButton:(UIButton *)sender{
    sender.selected = !sender.selected;
    if(sender.selected){
        switch (sender.tag - 10) {
            case 0:{
                [self remakeconstrains];
            }
                break;
            case 1:{
                //移除约束  （貌似没有什么用）
//                [self.sizeContraint uninstall];
                [self updateconstrains];
            }
                break;
            case 2:{
                [self addlabletextVC];
            }
                break;
            case 3:{
                
            }
                break;
            default:
                break;
        }
    }else{
        [self.redView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view).with.offset(SJHeight + 30) ;
            make.centerX.equalTo(self.view);
            make.size.mas_equalTo(CGSizeMake(300, 300));
        }];
    }
}

- (void)addtestView{
    self.redView = [[UIView alloc]initWithFrame:CGRectZero];
    self.redView.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.redView];
    [self.redView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).with.offset(SJHeight + 30) ;
        make.centerX.equalTo(self.view);
        //此处保存约束
        self.sizeContraint = make.size.mas_equalTo(CGSizeMake(300, 300));
    }];
}
- (void)addlabletextVC{
    updataLayoutViewController *layoutVC = [[updataLayoutViewController alloc]init];
    [self.navigationController pushViewController:layoutVC animated:YES];
}

#pragma mark - 重置约束
- (void)remakeconstrains{
    [self.redView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).with.offset(SJHeight + 30) ;
        make.centerX.equalTo(self.view);
        make.size.mas_equalTo(CGSizeMake(150, 150));
    }];
}
#pragma mark - 更新约束
- (void)updateconstrains{
    //添加约束 （貌似没什么用）
//    [self.sizeContraint install];
    [self.redView mas_updateConstraints:^(MASConstraintMaker *make) {
        //此处更新约束
        self.sizeContraint = make.size.mas_equalTo(CGSizeMake(100, 100));
        //或者
//        make.size.mas_equalTo(CGSizeMake(100, 100));
    }];
}




@end
